﻿using Microsoft.EntityFrameworkCore;

namespace EMall.Query
{
    public static class QueryablePagingExtension
    {
        public static IPagedList<TSource> ToPagedList<TSource>(this IQueryable<TSource> source, int pageIndex = 1, int pageSize = 10) =>
            source.ToPagedListAsync(pageIndex, pageSize).Result;
        public async static Task<IPagedList<TSource>> ToPagedListAsync<TSource>(this IQueryable<TSource> source, int pageIndex = 1, int pageSize = 10)
        {
            var totalCount = await source.CountAsync();
            var items = await source.Paging(pageIndex, pageSize).ToListAsync();
            return new PagedList<TSource>(pageIndex, pageSize, totalCount, items);
        }
        public static IQueryable<TSource> Paging<TSource>(this IQueryable<TSource> source, int pageIndex = 1, int pageSize = 10)
        {
            return source.Skip((pageIndex - 1) * pageSize).Take(pageSize);
        }
    }
}
